<?php
/*
Plugin Name: CDIA Widget Example
Description: Demonstration of the Widgets API for WV308
*/


/**
 * Adds CDIA_Example_Widget widget.
 */
class CDIA_Example_Widget extends WP_Widget {

	/**
	 * Register widget with WordPress.
	 */
	public function __construct() {

		$widget_args = array( 'description' => 'A simple example widget developer for the WV308 Project IV module.' );

		parent::__construct(
	 		'cdia_example_widget', // Base ID (as in HTML id="cdia_example_widget"
			'CDIA Example Widget', // Name of your widget (shown on the widgets admin page)
			$widget_args // Extra arguments
		);

	}

	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget( $args, $instance ) {
		
		// Grab the "title" data for this widget instance
		$title = $instance['title'];
		
		// Output content that should appear before the widget
		echo $args['before_widget'];
		
		// Check for a valid title before outputting it
		if ( ! empty( $title ) ) {
		
			// Output markup for before title, followed by title, followed by markup for after title	
			echo $args['before_title'] . $title . $args['after_title'];
		
		}
		
		// Custom widget content body goes here
		echo 'Copywrite 2012 | BU CDIA';
		
		// Close out the widget display with content designated for after_widget display
		echo $args['after_widget'];
		
	}

	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 *
	 * @return array Updated safe values to be saved.
	 */
	public function update( $new_instance, $old_instance ) {
	
		$instance = array();
		
		// Clean data before handing it off for insertion in the database:
		// In this case, I don't want any HTML tags (<p>,<h1>, etc.) present
		// in the widget title because I will handle the markup myself.
		// Therefore I use the PHP strip_tags function to clean the data first
		$instance['title'] = strip_tags( $new_instance['title'] );

		// ALWAYS return an associative array where key = input field and value =
		// sanitized user submitted value for each widget input field.
		return $instance;
		
	}

	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 */
	public function form( $instance ) {
		
		// Look for pre-existing values for the title field
		if ( isset( $instance[ 'title' ] ) ) {
			
			// If one exists, remember it for later
			$title = $instance[ 'title' ];
			
		} else {
			
			// Otherwise, set a sensible default value
			$title = 'New title';
			
		}
		
		// Echo the HTML markup for our widget admin form
		?>
   		<p>
		<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label> 
		<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
		</p>
		<?php
		
	}

} // class CDIA_Example_Widget

// Registration of custom widgets must happen in a callback for the 'widgets_init' action
add_action( 'widgets_init', 'cdia_widget_example_register_widget' );

/**
 * Register our custom widget
 */ 
function cdia_widget_example_register_widget() {

	// Tell WordPress we have a widget ready for use
	register_widget( 'CDIA_Example_Widget' );

}